﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace UTL
{
    /// <summary>
    /// Session login
    /// </summary>
    public sealed class Session
    {
        /// <summary>
        /// License's software
        /// </summary>
        public enum LicState { Unlimited, Trial, None }

        /// <summary>
        /// All perform status of DAL
        /// </summary>
        public enum PerState { Empty, Duplicate, Uninsert, Unupdate, Undelete, Unfind, Success }

        /// <summary>
        /// State of form input
        /// </summary>
        public enum FormState { Add, Edit }

        /// <summary>
        /// Roles of user login
        /// </summary>
        public enum Roles { Admin = 1, Manager, User1, User2, None };

        private bool _login;
        private string _name;

        public long Id { set; get; }
        public string Acc { set; get; }
        public string Pass { set; get; }
        public Roles Role { set; get; }
        public DateTime? Current { set; get; }
        public LicState License { set; get; }

        public string Name
        {
            set { _name = value; }
            get
            {
                if (License == LicState.Trial) _name += " - Phiên bản dùng thử";
                return _name;
            }
        }

        public bool Login
        {
            set
            {
                if (value == false)
                {
                    Id = -1;
                    Acc = null;
                    Pass = null;
                    Name = "Đăng nhập để làm việc";
                    Role = Roles.None;
                    License = LicState.None;
                }
                _login = value;
            }
            get
            {
                return _login;
            }
        }

        public Session()
        {
            Login = false;

            // Check license
            var key = (new UTL.Hasher.Registri()).Read("License");
            License = UTL.Hasher.License.IsLincense(key);
        }
    }
}